package com.jarveis.frame.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * 文件工具类
 * 
 * @author liuguojun
 */
public class FileUtil {

	private static final Logger logger = Logger.getLogger(FileUtil.class);

	/**
	 * 创建目录
	 * 
	 * @param folderPath
	 *            文件夹路径
	 * @return {success:1, failed:-1}
	 */
	public static int createFolder(String folderPath) {
		int reInt = -1;

		try {
			File folder = new File(folderPath);
			if (!folder.exists()) {
				folder.mkdirs();
				reInt = 1;
			}
		} catch (Exception ex) {
			logger.error("创建文件夹出错", ex);
		}

		return reInt;
	}

	/**
	 * 创建文件
	 * 
	 * @param filePath
	 *            文件路径
	 * @param fileContent
	 *            文件内容
	 * @return {success:1, failed:-1}
	 */
	public static int createFile(String filePath, String fileContent) {
		int reInt = -1;

		try {
			File file = new File(filePath);
			if (!file.exists()) {
				file.createNewFile();
			}
			if (file.canWrite()) {
				PrintWriter pw = new PrintWriter(new FileWriter(file));
				pw.println(fileContent);
				pw.close();
				reInt = 1;
			}
		} catch (Exception ex) {
			logger.error("创建文件出错", ex);
		}

		return reInt;
	}

	/**
	 * 删除文件
	 * 
	 * @param filePath
	 *            文件路径
	 * @return {success:1, failed:-1}
	 */
	public static int deleteFile(String filePath) {
		int reInt = -1;

		try {
			File file = new File(filePath);
			if (file.exists()) {
				file.delete();
				reInt = 1;
			}
		} catch (Exception ex) {
			logger.error("删除文件出错", ex);
		}

		return reInt;
	}

	/**
	 * 删除文件夹
	 * 
	 * @param folderPath
	 *            文件夹路径
	 * @return {success:1, failed:-1}
	 */
	public static int deleteFolder(String folderPath) {
		int reInt = -1;

		try {
			// 删除完里面所有内容
			deleteFiles(folderPath);
			// 删除空文件夹
			File folder = new File(folderPath);
			if (folder.exists()) {
				folder.delete();
				reInt = 1;
			}
		} catch (Exception ex) {
			logger.error("删除文件夹出错", ex);
		}

		return reInt;
	}

	/**
	 * 删除文件夹内的所有文件
	 * 
	 * @param folderPath
	 *            文件夹路径
	 */
	public static void deleteFiles(String folderPath) {
		File folder = new File(folderPath);
		if (folder.exists() && folder.isDirectory()) {
			String[] fileNames = folder.list();
			File file = null;
			for (int i = 0; i < fileNames.length; i++) {
				if (folderPath.endsWith(File.separator)) {
					file = new File(folderPath + fileNames[i]);
				} else {
					file = new File(folderPath + File.separator + fileNames[i]);
				}
				if (file.isFile()) {
					file.delete();
				}
				if (file.isDirectory()) {
					// 先删除文件夹里面的文件
					deleteFiles(folderPath + File.separator + fileNames[i]);
					// 再删除空文件夹
					deleteFolder(folderPath + File.separator + fileNames[i]);
				}
			}
		}
	}

	/**
	 * 复制文件
	 * 
	 * @param source
	 *            源文件
	 * @param target
	 *            目标文件
	 */
	public static void copyFile(File source, File target) {
		if (source.exists() && source.isFile() && source.canRead()) {
			FileInputStream fis = null;
			FileOutputStream fos = null;
			try {
				fis = new FileInputStream(source);
				fos = new FileOutputStream(target);
				IOUtil.write(new BufferedInputStream(fis),
						new BufferedOutputStream(fos));
			} catch (Exception ex) {
				logger.error("复制文件出错", ex);
			} finally {
				IOUtil.close(fos);
				IOUtil.close(fis);
			}
		}
	}

	/**
	 * 复制文件
	 * 
	 * @param source
	 *            源文件
	 * @param target
	 *            目标文件
	 */
	public static void copyFile(String source, String target) {
		copyFile(new File(source), new File(target));
	}

	/**
	 * 复制文件夹
	 * 
	 * @param source
	 *            源文件夹
	 * @param target
	 *            目标文件夹
	 */
	public static void copyFolder(String source, String target) {
		try {
			(new File(target)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
			final String[] filePaths = new File(source).list();
			File temp = null;
			for (int i = 0; i < filePaths.length; i++) {
				if (source.endsWith(File.separator)) {
					temp = new File(source + filePaths[i]);
				} else {
					temp = new File(source + File.separator + filePaths[i]);
				}

				// 忽略svn文件夹
				if (".svn".equals(temp.getName())) {
					continue;
				}

				if (temp.isFile()) {
					copyFile(temp.getPath(), target + File.separator
							+ temp.getName());
				}
				if (temp.isDirectory()) {// 如果是子文件夹
					copyFolder(source + File.separator + filePaths[i], target
							+ File.separator + filePaths[i]);
				}
			}
		} catch (Exception ex) {
			logger.error("复制文件夹出错", ex);
		}
	}

	/**
	 * 移动文件
	 * 
	 * @param source
	 *            源文件
	 * @param target
	 *            目标文件
	 */
	public static void moveFile(String source, String target) {
		copyFile(source, target);
		deleteFile(source);
	}

	/**
	 * 移动文件夹
	 * 
	 * @param source
	 *            源文件夹
	 * @param target
	 *            目标文件夹
	 */
	public static void moveFolder(String source, String target) {
		copyFolder(source, target);
		deleteFolder(source);
	}

	/**
	 * 列出目录下的所有文件的路径
	 * 
	 * @param filePath
	 * @return
	 */
	public static List<String> list(String filePath) {
		List<String> filePaths = new ArrayList<String>();

		File folder = new File(filePath);
		if (folder.exists() && folder.canExecute()) {
			if (folder.isDirectory()) {
				File[] arr = folder.listFiles();
				for (int i = 0; i < arr.length; i++) {
					String path = arr[i].getPath();
					filePaths.add(path);
					filePaths.addAll(list(path));
				}
			}
		}

		return filePaths;
	}
}
